package com.smarttest.portal.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarttest.common.beans.JsonDataResult;
import com.smarttest.common.beans.PaginationResult;
import com.smarttest.portal.service.Autocase_Mgmt_Service;

@RequestMapping("/AutoProtocol")
@Controller
public class AutoProtolcolController {
	private final Logger log = Logger.getLogger(getClass());
	@Autowired
	@Qualifier("msgAuto")
	private Autocase_Mgmt_Service caseMgmtService;

	@RequestMapping(value = "/search/table.action", method = {
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	@ResponseBody
	public PaginationResult searchTest(String name, String classes, String scenes, String owners, String searchvalue,
			int level, int pageSize, int toPage) {
		PaginationResult result = caseMgmtService.searchCase(name, classes, scenes, owners, searchvalue, level,
				pageSize, toPage);
		return result;
	}

	@RequestMapping(value = "/case/add.action", method = {
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	@ResponseBody
	public JsonDataResult addTest(long id, String name, String clazz, String scenes, String owner, String remark,
			int level,String url) {
		int resultInt = 0;
		if (id > 0) {
			resultInt = caseMgmtService.updateCase(id, name, clazz, scenes, owner, remark, level,url);
		} else {
			resultInt = caseMgmtService.createCase(name, clazz, scenes, owner, remark, level,url);
		}
		if (resultInt > 0) {
			return JsonDataResult.ok();
		} else {
			return JsonDataResult.error(200);
		}
	}

	@RequestMapping(value = "/case/delete.action", method = {
			RequestMethod.POST }, produces = "application/json; charset=utf-8")
	@ResponseBody
	public JsonDataResult deleteTest(String idsStr) {
		if (idsStr == null)
			return JsonDataResult.error(300);
		String[] idsarr = idsStr.split(",");
		if (idsarr.length <= 0) {
			return JsonDataResult.error(300);
		}
		List<Long> ids = new ArrayList<Long>();
		for(String each:idsarr){
			ids.add(Long.parseLong(each));
		}
		int result = caseMgmtService.deleteCase(ids);
		return JsonDataResult.ok(result);
	}
}
